var wxCharts = require('../../../utils/wx_charts.js')    // 根据wxCharts所在的位置而定
var Function = require("../../../utils/function.js");
import request from '../../../utils/request.js'
var yuelineChart = null
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0 ,//索引
    start_date : '0000-00-00',
    end_date : '0000-00-00',
    hou_start : '',
    hou_end : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  this. getDetail();
  },
  bindDateChange(e){  //开始时间
  this.setData({
     start_date : e.detail.value,
     hou_start : e.detail.value
  })
  },
  bindDateChange2(e){ //结束时间
    if(this.data.start_date == '0000-00-00'){
      Function.layer('请先选择开始时间！')
      return false;
    }
     this.setData({
       end_date :e.detail.value,
       hou_end : e.detail.value
     })

     this.getDetail()//获取收益详情
  },

  forMoreInfoTap: function () { //回退一页
    wx.navigateBack({
      delta: 1
    })
  },
  createSimulationData() { //获取动态值
    var amount = this.data.detail.line_chart.amount;
    var days = this.data.detail.line_chart.days;
    return {
      amount: amount,
      days: days
    }
  },
  // 线图
  getMothElectro1: function () {
    var simulationData = this.createSimulationData();
    yuelineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: simulationData.days,
      animation: true,
      background: '#f5f5f5',
      series: [{ // 数据列表
        name: '',
        data: simulationData.amount,
        format: function (val, name) {
          return val;
        }
      }
    ],
      xAxis: {
        disableGrid: true
      },
      width: 310,
      height: 200,
      dataLabel: true,   // 是否在图表中显示数据内容值
      legend: false,     // 是否显示图表下方各类别的标识
      dataPointShape: false,
      enableScroll: true,
      extra: {
        lineStyle: 'straight'
      }
    });
  },
  touchHandler: function (e) {
    yuelineChart.scrollStart(e);
  },
  moveHandler: function (e) {
    yuelineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    yuelineChart.scrollEnd(e);
  },
  getDetail(){
    var token = wx.getStorageSync('token') || '';
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '为了保证您的信息安全，请先授权登录！',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/authorization/auth/auth',
            })
          }
        }
      })
    } else {
      request({
        url: '/api/jiameng.index/getPerformanceInfo',
        data: {
          token: token,
          start_date : this.data.hou_start,
          end_date : this.data.hou_end
        },
        header: {
          token: token
        }
      }).then(res => {
        console.log(res, '收益详情')
        if (res.data.code == 200) {
          this.setData({
           detail : res.data.data
          })
          this.getMothElectro1() //加载折线图
        } else {
          Function.layer('获取店铺收益详情失败，请稍后重试！')
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    setTimeout(()=> {
        this.setData({
            hou_end : '',
            hou_start : '',
            start_date : '0000-00-00',
            end_date : '0000-00-00'
          })
    this.getDetail();
      wx.stopPullDownRefresh()
    }, 1500)
  }
})